﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConfigGen.Core.ConfigurationSettings
{
    /// <summary>
    /// Represents the configuration settings for a single machine. This class is immutable.
    /// </summary>
    [Serializable]
    public class MachineConfigurationSettings
    {
        #region Private Members
        private readonly string _machineName;
        private readonly string _configFilePath;
        private readonly MachinesConfigurationCollection _parentConfigurationCollection;
        private readonly ConfigurationSetting[] _configurationSettings;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="MachineConfigurationSettings"/> class.
        /// </summary>
        /// <param name="machineName">Name of the machine.</param>
        /// <param name="configFilePath">The config file path.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="parentConfigurationCollection">The parent configuration collection.</param>
        /// <exception cref="ArgumentNullException">Raised if any of <paramref name="machineName"/>, <paramref name="configFilePath"/> or <paramref name="settings"/> are null.</exception>
        /// <exception cref="ArgumentException">Raised if <paramref name="machineName"/> or <paramref name="configFilePath"/> are zero length.</exception>
        public MachineConfigurationSettings(
            string machineName, 
            string configFilePath, 
            IEnumerable<ConfigurationSetting> settings,
            MachinesConfigurationCollection parentConfigurationCollection)
        {
            if (machineName == null) throw new ArgumentNullException("machineName");
            if (machineName.Length == 0) throw new ArgumentException("machineName");
            if (configFilePath == null) throw new ArgumentNullException("configFilePath");
            if (configFilePath.Length == 0) throw new ArgumentException("configFilePath");
            if (settings == null) throw new ArgumentNullException("settings");

            _machineName = machineName;
            _configurationSettings = settings.ToArray();
            _configFilePath = configFilePath;
            _parentConfigurationCollection = parentConfigurationCollection;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MachineConfigurationSettings"/> class.
        /// </summary>
        /// <param name="machineName">Name of the machine.</param>
        /// <param name="configFilePath">The config file path.</param>
        /// <param name="settings">The settings.</param>
        public MachineConfigurationSettings(
            string machineName,
            string configFilePath,
            IEnumerable<ConfigurationSetting> settings)
            : this (machineName, configFilePath, settings, null)
        {

        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Returns true if the this collection of configuration settings contains a setting for the specified token, otherwise false.
        /// </summary>
        /// <param name="token">Token name of setting to search for.</param>
        /// <returns>true if the this collection of configuration settings contains a setting for the specified token, otherwise false</returns>
        public bool Contains(string token)
        {
            return GetConfigurationSettingInternal(token) != null;
        }

        /// <summary>
        /// Gets the configuration setting matching the supplied token, if any, otherwise null.
        /// </summary>
        /// <param name="token">Token of setting to retrieve (case insensitive)</param>
        /// <returns>Matching token, or null if no match.</returns>
        public ConfigurationSetting GetConfigurationSetting(string token)
        {
            var setting = GetConfigurationSettingInternal(token);
            if (setting == null)
            {
                return null;
            }
            return new ConfigurationSetting(setting.Token, setting.Value, this);
        }

        /// <summary>
        /// Gets the configuration setting matching the supplied token, if any, otherwise null.
        /// </summary>
        /// <param name="token">Token of setting to retrieve (case insensitive)</param>
        /// <returns>Matching token, or null if no match.</returns>
        public ConfigurationSetting this [string token]
        {
            get { return GetConfigurationSetting(token);}
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the collection of settings for this machine.
        /// </summary>
        public ConfigurationSetting[] ConfigurationSettings
        {
            get { return _configurationSettings; }
        }

        /// <summary>
        /// Gets the name of the machine to which this settings collection pertains.
        /// </summary>
        public string MachineName
        {
            get { return _machineName; }
        }

        /// <summary>
        /// Gets the relative path and name of the configuration file ot be generated for this machine.
        /// </summary>
        public string ConfigFilePath
        {
           get { return _configFilePath; }
        }

        /// <summary>
        /// Gets the parent configuration collection, being the collection of machine settings for all machines.
        /// </summary>
        public MachinesConfigurationCollection ParentConfigurationCollection
        {
            get { return _parentConfigurationCollection; }
        }

        #endregion

        #region Private Properties
        private ConfigurationSetting GetConfigurationSettingInternal(string token)
        {
            return _configurationSettings.FirstOrDefault(s => string.Equals(s.Token, token));
        }
        #endregion
    }
}
